import java.util.Arrays;
//哈希桶里采用的是头插法
public class HashBuck {
    public static class Node{
        public int key;
        public int val;
        public Node next;
        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node[]array=new Node[10];
    public int usedSize;
    public static final double DEFAULT_LOAD_FACTOR=0.75f;

    public void push(int key,int val){
        int index=key % array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                cur.val=val;
                return;
            }
            cur=cur.next;
        }
        Node node=new Node(key,val);
        node.next=array[index];
        array[index]=node;
        usedSize++;
        if(doLoadFactor()>=DEFAULT_LOAD_FACTOR){
            resize();
        }
    }

    public  int getVal(int key){
        int index=key%array.length;
        Node cur=array[index];
        while (cur!=null){
            if(cur.key==key){
                return cur.val;
            }
            cur=cur.next;
        }
        return -1;
    }


    public double doLoadFactor(){
        return usedSize*1.0/array.length;
    }
    private void resize() {
        Node[] newarray= Arrays.copyOf(array,2*array.length);
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while (cur!=null){
                int newIndex=cur.key%newarray.length;
                Node curN=cur.next;
                cur.next=newarray[newIndex];
                newarray[newIndex]=cur;
                cur=curN;
            }
        }
        array=newarray;
    }
}
